package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.Question;
import cn.xgj.sfzy.service.QuestionService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName QuestionController
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月24日 上午10:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @RequestMapping("/findAll")
    public Result findAll(Question question){
        List<Question> list = questionService.findAll(question);
        return Result.success(list);
    }
    @RequestMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "2") Integer pageSize,
            Question question){
        PageInfo<Question> byPage = questionService.findByPage(pageNum, pageSize, question);
        return Result.success(byPage);
    }

    @PostMapping
    public Result add(@RequestBody Question question){
        int num = questionService.add(question);
        if(num!=0){
            return Result.success("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody Question question){
        int num = questionService.edit(question);
        if(num!=0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{questionId}")
    public Result delete(@PathVariable(value = "questionId") Integer questionId){
        int num = questionService.remove(questionId);
        if(num!=0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    @GetMapping("/{questionId}")
    public Result findById(@PathVariable(value = "questionId") Integer questionId){
        Question question = questionService.findById(questionId);
        return Result.success(question);
    }
    
}
